package com.gfxy.modules.index.controller;

import com.alibaba.fastjson.JSONObject;
import com.gfxy.modules.index.service.SchoolIndexService;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2021/9/12 5:54 下午
 */
@Api(tags="全校首页报表")
@RestController
@RequestMapping("index/schoolIndex")
@Slf4j
public class SchoolIndexController {

    @Autowired
    private SchoolIndexService schoolIndexService;

    @AutoLog(value = "按年查询课时信息")
    @ApiOperation(value="按年查询课时信息", notes="按年查询课时信息")
    @GetMapping(value = "/listClasstimeByYear")
    public Result<?> listClasstimeByYear(@RequestParam(name="date",required=true) String date,
                                   HttpServletRequest req) {
        log.debug("开始查询{}年的课时信息",date);
        Map<String, List> result = schoolIndexService.getTotalClasstimeByYear(date);
        return Result.OK(result);
    }

    @AutoLog(value = "按年查询课时类型占比")
    @ApiOperation(value="按年查询课时类型占比", notes="按年查询课时类型占比")
    @GetMapping(value = "/classTimeProportionByYear")
    public Result<?> classTimeProportionByYear(@RequestParam(name="date",required=true) String date,
                                   HttpServletRequest req) {
        log.debug("开始查询{}年的课时信息",date);
        List<SchoolIndexService.KeyValue> proportions = schoolIndexService.getClassTimeProportionByYear(date);
        Map<String,Object> result = new HashMap<>();
        result.put("proportions",proportions);

        double d=proportions.stream().mapToDouble(SchoolIndexService.KeyValue::getValue).sum();
        BigDecimal b = new BigDecimal(d);
        double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        result.put("total",Double.valueOf(f1));
        return Result.OK(result);
    }

    @AutoLog(value = "按年查询教学建设")
    @ApiOperation(value="按年查询教学建设", notes="按年查询教学建设")
    @GetMapping(value = "/teachingDefaultByYear")
    public Result<?> teachingDefaultByYear(@RequestParam(name="date",required=true) String date,
                                               HttpServletRequest req) {
        log.debug("开始查询{}年的课时信息",date);
        JSONObject result = this.schoolIndexService.getTeachingDefaultView(date);
        return Result.OK(result);
    }

}
